package com.zjf.addresschk.service;

import com.google.maps.GeoApiContext;
import com.google.maps.GeocodingApi;
import com.google.maps.model.AddressComponent;
import com.google.maps.model.GeocodingResult;
import com.zjf.addresschk.entity.AddressEntity;
import com.zjf.addresschk.entity.InfGoogle;

import java.util.List;

/**
 * @Title:AddressChkServiceGoogle
 * @Description:
 * @Author: zhaojunfu
 * @Date: 2015/5/25 15:06
 */
public class AddressChkServiceGoogle extends   AddressChkServiceBase {
    private InfGoogle inf;

    public AddressChkServiceGoogle(InfGoogle inf) {
        this.inf = inf;
    }

    @Override
    public void checkAddress(List<AddressEntity> addressList) {
        GeoApiContext context = new GeoApiContext().setApiKey(inf.getApiKey());
        int i = 1;
        for(AddressEntity a:addressList) {
            try {
                GeocodingResult[] results = new GeocodingResult[0];
                results = GeocodingApi.geocode(context,
                        a.getAddress()).await();
//                System.out.println(results[0].formattedAddress);
                a.setChkResult(results.length>0?"有效:谷歌检测结果["+results[0].formattedAddress+"]":"无效");
                setPostCode(a, results);
                System.out.println("第" + i++ + "个检测成功");
            } catch (Exception e) {
                //e.printStackTrace();
                String info ="" ;
                if(e instanceof IndexOutOfBoundsException){
                    info ="未查询到地址";
                }else{
                    info ="网路链接失败";
                }
                System.out.println("第" + i++ + "个检测失败:"+info);
            }
        }

    }

    private void setPostCode(AddressEntity a, GeocodingResult[] results) {
        AddressComponent[] acs = results[0].addressComponents;
        for(AddressComponent ac:acs){
            if("postal_code".equalsIgnoreCase(ac.types[0].toString())){
                a.setPostcode(ac.longName);
            }
        }
    }

    @Override
    public void catchPostcode(List<AddressEntity> addressList) {
        //在检测过程中已经顺便获取邮编
    }

    /*
   {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Pkwy",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.4224764,
               "lng" : -122.0842499
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4238253802915,
                  "lng" : -122.0829009197085
               },
               "southwest" : {
                  "lat" : 37.4211274197085,
                  "lng" : -122.0855988802915
               }
            }
         },
         "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}
    */
}
